package Search.search2017;

/**
 * 插值查找,对于分布较均匀的有序排列数组查找优于二分查找，但不适于分布不均匀的,
 * 时间复杂度O(logn)。
 * Created by 雷文康 on 2017/12/27.
 */
public class InsertSearch {
    public static void main(String[] args) {
        int[] array={1,2,4,5,25,63,72,81};
        System.out.println(insertSearch(array,25));
    }
    public static int insertSearch(int[] array, int num) {
        if (array == null || array.length == 0){
            return -1;
        }
        int low = 0;
        int high = array.length - 1;
        int mid;
        while (low <= high) {
            mid = low + ((num - array[low]) / (array[high] - array[low])) * (high - low);
            if (num == array[mid]) {
                return mid;
            } else if (num < array[mid]) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return  -1;
    }
}
